var serviceUrl = $api.getStorage('serviceUrl');  //服务器地址
	


var Login = function(account, password) {
	account = account.trim();
	password = password.trim();

	do_ajax(serviceUrl + '/app/member/login', 'post', {
		account: account,
		password: password
	}, function(result, err) {
		if (result.resultCode == '0') {
			$api.setStorage('account', account);
			$api.setStorage('password', password);
			getMemberInfo(account);
		}
	});
}

//打开窗口
var openWin = function(winName, pageParam) {
	//	var delay = 0;
	//  if("ios" != api.systemType){
	//      delay = 300;
	//  }
	api.openWin({
		name: winName,
		url: winName + ".html",
		pageParam: pageParam,
		bounces: false,
		vScrollBarEnabled: false,
		hScrollBarEnabled: false,
		reload: true,
		// delay: delay
	});
}

//http请求
var do_ajax = function(url, method, data, cb) {

	// alert(JSON.stringify(data));
	// serviceUrl = "http://192.168.0.128:7081"
	var connectionType = api.connectionType; //比如: wifi
	if (connectionType == 'none') {
		toast('当前无网络访问!');
		return;
	}
	api.showProgress({
		title: '加载中...',
		modal: false
	});
	api.ajax({
		url: serviceUrl + url,
		method: method,
		dataType: 'json',
		returnAll: false,
		cache: false,
		timeout: 15,
		headers: {
			"Content-type": "application/json;charset=UTF-8"
		},
		data: {
			body: data
		}
	}, function(result, err) {
		cb(result, err);
	});


};


//获取用户信息
var getMemberInfo = function(account) {
	api.ajax({
		url: serviceUrl + '/app/community/userinfo',
		method: 'post',
		timeout: 30,
		dataType: 'json',
		returnAll: false,
		cache: false,
		timeout: 20,
		headers: {
			"Content-type": "application/json;charset=UTF-8"
		},
		data: {
			body: {
				account: account
			}
		}
	}, function(result, err) {
		if (result) {
			//发送更新获取用户信息监听事件
			api.sendEvent({
				name: 'refreshuser',
			});
		} else {
			toast('加载失败!');
		}
	});
}


//获取收货地址
var getAddress = function(cb) {

	var member = $api.getStorage('member');

	api.showProgress({
		title: '加载中...',
		modal: false
	});
	var param = '?account=' + member.account;
	var url = serviceUrl + '/app/member/addresslist' + param;

	$api.get(url, function(ret) {
		api.hideProgress();
		cb(ret);

	}, 'json');



}


//添加收货地址
var addaddress = function(Param) {
	api.openWin({
		name: 'myaddressEdit',
		url: "myaddressEdit.html",
		reload: true,
		opaque: true,
		vScrollBarEnabled: false,
		pageParam: Param
	});
}


//购物车添加商品
var addCratItem = function(proId) {
	var member = $api.getStorage('member');

	if (!member) {
		api.openWin({
			name: 'login',
			url: 'login.html',
			opaque: true,
			vScrollBarEnabled: false
		});
		return;
	}

	api.showProgress({
		title: '加载中...',
		modal: false
	});

	api.ajax({
		url: serviceUrl + '/app/cart/add',
		method: 'post',
		timeout: 30,
		dataType: 'json',
		returnAll: false,
		cache: false,
		timeout: 20,
		headers: {
			"Content-type": "application/json;charset=UTF-8"
		},
		data: {
			body: {
				buyNum: 1,
				proId: proId,
				cartId: member.cartId
			}
		}
	}, function(result, err) {
		if (result.resultCode == '0') {
			var carId = result.cartkey;
			if (carId) {
				member.cartId = carId;
				$api.setStorage('member', member);
			}

			//发送刷新购物车监听事件
			api.sendEvent({
				name: 'refreshCart',
				// extra: address
			});
			api.sendEvent({
				name: 'getCartDate',
				// extra: address
			});

			//				    api.toast({
			//							    msg: '成功加入购物车',
			//							    duration:3000,
			//							    location: 'bottom'
			//							});
			toast('成功加入购物车!');

		} else {
			toast('加入购物车失败!');
		}
		api.hideProgress();

	});

}


//根据下标返回数组的数据
var findIndex = function(list, id) {
	var index = -1;

	for (var i = 0; i < list.length; i++) {
		if (list[i].id == id) {
			index = i;
			return index;
		}
	}
	return index;
}

var toast = function(msg) {
	api.toast({
		msg: msg,
		duration: 1500,
		location: 'middle'
	});
}

Date.prototype.format = function(fmt) { //author: meizz   
	var o = {
		"M+": this.getMonth() + 1, //月份   
		"d+": this.getDate(), //日   
		"h+": this.getHours(), //小时   
		"m+": this.getMinutes(), //分   
		"s+": this.getSeconds(), //秒   
		"q+": Math.floor((this.getMonth() + 3) / 3), //季度   
		"S": this.getMilliseconds() //毫秒   
	};
	if (/(y+)/.test(fmt))
		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o)
		if (new RegExp("(" + k + ")").test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
}